#-*- coding:utf-8 -*-

try:
    import maya.cmds as cmds
except Exception as test_ERR:
    print('test')


def auto_rename():
    meshs = cmds.ls(type='mesh', l=1)
    mesh_names = []

    count = 0
    for s in meshs:
        name = s.split('|')
        ns = name[:-1]
        mesh_name = name[-2]
        long_mesh_name = '|'.join(ns)
        if mesh_name not in mesh_names:
            mesh_names.append(mesh_name)
        else:
            new_mesh_name = mesh_name + '_' + str(count)
            if new_mesh_name in mesh_names:
                new_mesh_name += '_'
            cmds.rename(long_mesh_name, new_mesh_name)
            mesh_names.append(new_mesh_name)
            count += 1


def rename_detect():
    """
    重命名检测
    :return:
    """
    result = False

    meshs = cmds.ls(type='mesh', l=1)
    mesh_names = []
    repeat_meshs = []
    for s in meshs:
        name = s.split('|')
        ns = name[:-1]
        mesh_name = name[-2]
        long_mesh_name = '|'.join(ns)
        if mesh_name not in mesh_names:
            mesh_names.append(mesh_name)
        else:
            repeat_meshs.append(long_mesh_name)

    print('repeat_meshs:', repeat_meshs)
    if repeat_meshs:
        show_list = '\n'.join(repeat_meshs)

        choose = cmds.confirmDialog(title='result', message=u'重命名物体:\n' + show_list, icon='warning',
                                    button=[u'自动重命名', u'取消'], defaultButton=u'取消', cancelButton=u'取消',
                                    dismissString=u'取消')  # , dismissString=u'取消'
        if choose == u'自动重命名':
            auto_rename()
            result = True
    else:
        result = True

    return result

def _run():
    try:
        rename_detect()
    except Exception as rename_detect_ERR:
        print('rename_detect_ERR:',str(rename_detect_ERR))



def _doc():
    doc = {'name':u'重命名检测','doc':'对所有mesh进行重命名检测'}
    return doc